package org.cell.server.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Comparator;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.ActionMessage;
import flex.messaging.io.amf.Amf0Input;

@Controller
public class HomeController {
	
	@Resource(name = "caseInsensitiveComparator")
	Comparator<String> comparator;
	
    @RequestMapping(value = "/home")
    public String home() {
        System.out.println("hello");
        return "home";
    }
    //	http://127.0.0.1:8080/GameServer/compare?input1=hello&input2=jing
    @RequestMapping(value = "/compare", method = RequestMethod.GET)
    public String compare(@RequestParam("input1") String input1,
    		@RequestParam("input2") String input2, Model model){
    	int result = comparator.compare(input1, input2);
    	String inEnglish = (result < 0) ? "less than" : (result > 0 ? "greater than" : "equal to");
    	String output = "According to our Comparator, '" + input1 + "' is " + inEnglish + "'" + input2 + "'";

    	model.addAttribute("output", output);
    	return "compareResult";
    }
    
    //http://127.0.0.1:8080/GameServer/test?input1=hello&id=5&name=jing
    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    public String test1(String input1, User user){
    	System.out.println(input1);
    	System.out.println(user.getId()+"  "+user.getName());
    	return "";
    }
    
    @RequestMapping(value = "/test2", method = RequestMethod.POST)
    public String test2(HttpServletRequest req, HttpServletResponse res){
    	String id = req.getParameter("id");
    	String name = req.getParameter("name");
    	System.out.println(id+"    "+name);
    	return "";
    }
    
    @RequestMapping(value = "/test3", method = RequestMethod.POST)
    public String test3(HttpServletRequest req){
    	byte[] b = new byte[1024];
    	try {
			req.getInputStream().read(b);
			System.out.println();
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return "";
    }
    
    @RequestMapping(value = "/test4", method = RequestMethod.POST)
    public @ResponseBody String test4(HttpServletRequest req){
    	try {
			InputStream inputStream = req.getInputStream();
			ActionMessage inAm = new ActionMessage();
			Amf0Input amfIn = new Amf0Input(SerializationContext.getSerializationContext());
			amfIn.setInputStream(inputStream);
			
			try {
				Object object = amfIn.readObject();
				User user = (User)object;
				System.out.println(user.getId()+"  "+user.getName());
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return "";
    }
    
    public void decode(byte[] b){}
}

 